package com.xb.loan.encryptor.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

public class ToolUtil {

	public static final byte[] fileToBytes(String name) {

		File f = new File(name);

		if (!f.exists() || f.isDirectory()) {

			return null;
		}

		FileInputStream in = null;

		try {

			in = new FileInputStream(f);

			byte[] data = new byte[in.available()];

			in.read(data);

			return data;

		} catch (Exception e) {

			e.printStackTrace();
			
			return null;
		} finally {

			if (in != null) {

				try {
					in.close();
				} catch (IOException e) {

					e.printStackTrace();
				}
			}
		}

	}

	public static final void wirteIsAppend(String path, String msg,
                                           boolean isAppend) {

		FileWriter out = null;

		try {

			try {

				File file = new File(path);

				if (!file.exists()) {

					if (!file.getParentFile().exists()) {
						file.getParentFile().mkdirs();
					}

					file.createNewFile();

				}

				if (file.exists()) {
					out = new FileWriter(file, isAppend);
					out.write(msg);
					out.flush();
				}

			} finally {

				if (out != null) {
					out.close();
				}

			}

		} catch (Exception e) {

			e.printStackTrace();

		}

	}

}
